package csce315.group10.androidapp;

import csce315.group10.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

public class AddContactDialog extends Activity{
	/* private instance variables */
	public CustomApp appState;
	String position_text;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.addcontactdialog);
        appState = ((CustomApp)getApplicationContext());
        Spinner spinner = (Spinner) findViewById(R.id.Position_dropdown);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.positions_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new PositionSelectedListener());
    }
    
    public void okButtonHandler(View v){
    	/* gather the inputed information */
    	 EditText name = (EditText) findViewById(R.id.Get_name_text);
    	 String name_text = name.getText().toString();
         EditText phone = (EditText) findViewById(R.id.Get_phone_text);
         String phone_text = phone.getText().toString();
         EditText address = (EditText) findViewById(R.id.Get_address_text);
         String address_text = address.getText().toString();
         EditText email = (EditText) findViewById(R.id.Get_email_text);
         String email_text = email.getText().toString();
         /* check if contact already exists */
         for(int i = 0; i < appState.GetContacts().size(); i++){
        	 if(appState.GetContacts().get(i).get_name().equals(name_text)){
        		 Intent data = new Intent();
                 data.putExtra("exists", true);
        		 setResult(Activity.RESULT_OK, data);
        		 finish();
        	 }
         }
         /* build a new intent to return the data */
         Intent data = new Intent();
         data.putExtra("exists", false);
         data.putExtra("name", name_text);
         data.putExtra("phone", phone_text);
         data.putExtra("address", address_text);
         data.putExtra("email", email_text);
         data.putExtra("position", position_text);
         setResult(Activity.RESULT_OK, data);
    	 finish();
    }
    
    /* class that monitors the position selector */
    public class PositionSelectedListener implements OnItemSelectedListener {

        public void onItemSelected(AdapterView<?> parent,
            View view, int pos, long id) {
        	position_text = parent.getItemAtPosition(pos).toString();

        }

        public void onNothingSelected(AdapterView parent) {
          // Do nothing.
        }
    }
    
    //need to be completed
    public void setSpinnerPosition(int pos){
    	
    }
    
    public void setSpinnerSelection(String select){
    	
    }
    
    public int getSpinnerPosition(){
    	return 0;
    }
    
    public String getSpinnerSelection(){
    	return null;
    }
    
}